<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class ChangeController extends Controller
{
    public function __construct()
    {
        $this -> middleware('login');
    }
    public function itvadd()
    {
         return view('admin.change.itvadd',['title' => '广告位置']);
    }
    public function insert(Request $request)
    {
         $this -> validate($request,[
            'pic' => 'image|max:2048'
            ],[
            'pic.image' => '您上传的不是图片格式'
            ]);
          $data = $request -> only('name','money','url');
          if($request -> hasFile('pic'))
          {
            if($request -> file('pic') -> isValid())
            {
                  //处理头像
                  //获取文件后缀
                  $extension = $request -> file('pic') -> getClientOriginalExtension();

                  $filename = mt_rand(100000,999999999).'.'.$extension;

                  $dir = './uploads/itvpic/';

                  $request -> file('pic') -> move($dir, $filename);

                  $data['pic'] = '/uploads/itvpic/'.$filename;

                  $res = DB::table('itv') -> insert($data);

                  if($res)
                  {
                       return redirect('admin/change/itvindex') -> with(['info' =>'增加成功']);
                  }else
                  {
                       return back() -> with(['info' => '增加失败']);
                  }

            }
          }
    }
    public function index(Request $request)
    {

         $num = $request -> input('num',10);
         $keywords = $request -> input('keywords','');
         $data = DB::table('itv')
               -> where('name','like','%'.$keywords.'%')
               -> paginate($num);
         return view('admin.change.itvindex',['title'=>'广告主页','data'=> $data,'request' => $request -> all()]);
    }
    public function edit($id)
    {
         $data = DB::table('itv') -> where('id',$id) -> first();
         return view('admin.change.itvedit',['title' => '广告修改','data' => $data]);
    }
    public function update(Request $request)
    {
        $this -> validate($request,[
            'name' => 'min:2|max:18',
            'pic' => 'image|max:2048',
            ],[
            'name.min' => '广告主最少2位',
            'name.max' => '广告主最多18位',
            'pic.image' => '你必须传一张图片',
            'pic.max' => '最大2M'
            ]);

        $id = $request -> id;
        $data = $request -> except('_token','id','pic');
        $ispic = '';
        if($request -> hasFile('pic'))
         {

            if($request -> file('pic') -> isValid())
            {

              $extension = $request -> file('pic') -> getClientOriginalExtension();

              $filename = mt_rand(100000,999999999).'.'.$extension;

              $dir = './uploads/itvpic/';

              $request -> file('pic') -> move($dir, $filename);

              $data['pic'] = '/uploads/itvpic/'.$filename;

              $oldPic = DB::table('itv') -> where('id',$id) -> first() -> pic;

              //判断是不是默认值
              if(!empty($oldPic))
              {
                      if(file_exists('.'.$oldPic))
                      {
                        unlink('.'.$oldPic);
                      }
              }

            }

         }
         $res = DB::table('itv') -> where('id',$id) -> update($data);

         if($res)
         {
           return redirect('/admin/change/itvindex') -> with(['info' => '修改成功']);
         }else
         {
           return back() -> with(['info' => '修改失败']);
         }

    }
    public function delete($id)
    {
         $res = DB::table('itv') -> where('id',$id) -> delete();
         if($res)
         {
           return redirect('/admin/change/itvindex') -> with(['info' => '删除成功']);
         }else
         {
           return back() -> with(['info' => '删除失败']);
         }
    }

    public function itvName(Request $request)
    {
        $id = $request -> id;
        $data['name'] = $request -> input('name');
        $res = DB::table('itv') -> where('id',$id) -> update($data);
        if($res)
        {
             return 0;
        }else
        {
             return 1;
        }
    }
}